<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\controller\Admin;
use think\Db;

class Article extends Admin {

    /**
     * @title 文章框架
     */
    public function iframe() {
        // 加载内容
        $this->assign('iframe_src', url('admin/article/index'));
        // 加载分类
        $this->assign('nodes', model('article_cat')->iframe_category_tree_json());
        return $this->fetch('base:iframe');
    }

    /**
     * 文章列表
     */
    public function index() {

        $cid = input('get.category', NULL);


        session('redirect', url('index') . '?' . http_build_query(request()->get()));


        $lists = model('article')->model_where()->order('recommend desc,listorder asc,id desc')->paginate(config('base.page_size'), false, ['query' => request()->get()]);

        $this->assign('lists', $lists);
        $this->assign('page', $lists->render());

        // 分类转移 
        $this->assign('category_select', model('article_cat')->lists_select_tree());
        builder('list')
                ->addItem('id', '#')
                ->addSortItem('listorder', '排序', 'article')
                ->addItem('category_name', '分类')
                ->addItem('image', '主图', 'image')
                ->addItem('title', '标题')
                ->addItem('create_time', '时间')
                ->addItem('recommend', '推荐')
                ->addItem('alias', '别名')
                ->addAction('编辑', 'edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal')
                ->addAction('删除', 'delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();
        return view();
    }

    /**
     * @title 文章添加
     * @return type
     */
    public function add() {
        if (request()->isPost()) {

            $message = model('article')->add_submit(request()->post(), ['albums_update']);
            if ($message) {
                $this->error($message);
            } else {
                $this->success('', session('redirect'));
            }
        } else {
            
            
            $cid = input('get.cid', 0);
            
            builder('form')
                    ->addItem('cid', 'select', '分类', model('article_cat')->lists_select_tree())
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    // ->addItem('tags', 'input', '标签', '', '', '', '多个标签请用空格进行分隔')
                    ->addItem('image', 'image', '图片', '', '', '', '200x200')
                    ->addItem('albums', 'albums', '图集', [])
                    // ->addItem('file', 'input_select', '附件', 'admin/res/index?category=file')
                    ->addItem('keywords', 'input', 'seo关键词')
                    ->addItem('description', 'textarea', 'seo描述')
                    ->addItem('tpl_views', 'input_select', '模板', 'admin/index/tpl_select')
                    ->addItem('alias', 'input', '别名')
                    ->addItem('content', 'umeditor', '内容', '', 'style="height: 400px;"', 'simple')
                    ->addItem('smeta_json', 'smeta_json', '补充字段', config('smeta_json_article'))
                    ->build(['cid' => $cid]);
            return view();
        }
    }

    /**
     * @title 文章编辑
     * @return type
     */
    public function edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {


            $message = model('article')->edit_submit(request()->post(), $id, ['albums_update']);
            if ($message) {
                $this->error($message);
            } else {
                $this->success('', session('redirect'));
            }
        } else {            
            
            
            $one = db('article')->where('id', $id)->find();

            // 标签
            // $tags_arr = db('article_tags')->where('article_id', $id)->column('title');
            //  $one['tags'] = implode(' ', $tags_arr);

            builder('form')
                    ->addItem('cid', 'select', '分类', model('article_cat')->lists_select_tree())
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    //     ->addItem('tags', 'input', '标签', '', '', '', '多个标签请用空格进行分隔')
                    ->addItem('image', 'image', '图片', '', '', '', '200x200')
                    ->addItem('albums', 'albums', '图集', db('article_albums')->where('article_id', $id)->select())
                    //    ->addItem('file', 'input_select', '附件', 'admin/res/index?category=file')
                    ->addItem('keywords', 'input', 'seo关键词')
                    ->addItem('description', 'textarea', 'seo描述')
                    ->addItem('tpl_views', 'input_select', '模板', 'admin/index/tpl_select')
                    ->addItem('alias', 'input', '别名')
                    ->addItem('content', 'umeditor', '内容', '', 'style="height: 400px;"', 'simple')
                    ->addItem('smeta_json', 'smeta_json', '补充字段', config('smeta_json_article'))
                    ->build($one);
            return view();
        }
    }

    /**
     * @title 文章删除
     * @return type
     */
    public function delete($id) {
        empty($id) && $this->error('参数不能为空');

        if (db('article')->where('id', $id)->delete() !== FALSE) {
            $this->success('', url('index', ['category' => session('category')]));
        } else {
            $this->error('删除失败');
        }
    }
    
    /**
     * @title 文章多条删除
     * @param type $id
     */
    public function deletes($ids) {

        empty($ids) && $this->error('参数不能为空');

        $affect_row = db('article')->where('id', 'in', $ids)->delete();
        if ($affect_row) {
            $this->success('');
        } else {
            $this->error('删除失败');
        }
    }
    

    /**
     * @title 分类转移 
     */
    public function set_category() {
        $value = input('get.value', 0);
        $ids = input('post.ids');
        if (!empty($ids)) {

            $affect_rows = db('article')->where('id', 'in', $ids)->setField('cid', $value);
            $affect_rows ? $this->success('完成') : $this->success('没有更新任何数据');
        } else {
            $this->error('请选择要操作的数据');
        }
    }

    /**
     * @title 置顶 
     */
    public function set_top() {
        $value = input('get.value', 0);
        $ids = input('post.ids');
        if (!empty($ids)) {
            $affect_rows = db('article')->where('id', 'in', $ids)->setField('top', $value);
            $affect_rows ? $this->success('完成') : $this->success('没有更新任何数据');
        } else {
            $this->error('请选择要操作的数据');
        }
    }

    /**
     * @title 推荐 
     */
    public function set_recommend() {
        $value = input('get.value', 0);
        $ids = input('post.ids');
        if (!empty($ids)) {
            $affect_rows = db('article')->where('id', 'in', $ids)->setField('recommend', $value);
            $affect_rows ? $this->success('完成') : $this->success('没有更新任何数据');
        } else {
            $this->error('请选择要操作的数据');
        }
    }

    /**
     * @title 分类
     * @return type
     */
    public function category() {

        $lists = model('article_cat')->model_where()->select();
        
        
        // 转成树形
        $lists = collection($lists)->toArray();
        $tree = new \utils\Tools\Tree($lists);        

        $this->assign('lists', $tree->getArray(0));

        builder('list')
                ->addItem('id', '#')
                ->addSortItem('listorder', '排序', 'article_cat')
                ->addItem('title', '标题')
                ->addItem('tpl_lists:tpl_views', '列表/详情')
                ->addItem('alias', '别名')
                ->addItem('path', '路径')
                ->addAction('编辑', 'category_edit', '<i class="layui-icon layui-icon-edit"></i>', 'layui-btn-normal')
                ->addAction('删除', 'category_delete', '<i class="layui-icon layui-icon-delete"></i>', 'ajax-get confirm layui-btn-danger')
                ->build();

        return view();
    }

    /**
     * @title 分类添加
     * @return type
     */
    public function category_add() {

        if (request()->isPost()) {
            $post = input('post.');

            if ($insert_id = db('article_cat')->insertGetId($post)) {

                //添加PATH信息
                $pid = $post['pid'] == 0 ? "0" : $post['pid'];
                if (empty($pid)) {
                    $path = "0-$insert_id";
                } else {
                    $parent = db('article_cat')->where("id", $pid)->find();
                    $path = $parent['path'] . "-$insert_id";
                }
                db('article_cat')->where('id', $insert_id)->setField('path', $path);
                //添加PATH信息结束

                model('system_route')->route_reset();
                $this->success('', url('category'));
            } else {
                $this->error('没有更新');
            }
        } else {

            builder('form')
                    ->addItem('pid', 'select', '上级', model('article_cat')->lists_select_tree())
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('tpl_lists', 'input_select', '列表', 'admin/index/tpl_select')
                    ->addItem('tpl_views', 'input_select', '详情', 'admin/index/tpl_select')
                    ->addItem('alias', 'input', '别名')
                    ->addItem('keywords', 'input', 'seo关键词')
                    ->addItem('description', 'textarea', 'seo描述')
                    ->build();

            return view();
        }
    }

    /**
     * @title 分类编辑
     */
    public function category_edit($id) {

        empty($id) && $this->error('参数不能为空');

        if (request()->isPost()) {
            $post = input('post.');

            //编辑PATH信息
            $pid = empty($post['pid']) ? "0" : $post['pid'];
            if (empty($pid)) {
                $post['path'] = "0-" . $post['id'];
            } else {
                $parent = db('article_cat')->where("id", $pid)->find();
                $post['path'] = $parent['path'] . "-" . $post['id'];
            }
            //编辑PATH信息结束

            if (db('article_cat')->where('id', $id)->update($post) !== FALSE) {
                model('system_route')->route_reset();
                $this->success('', url('category'));
            } else {
                $this->error('没有更新');
            }
        } else {

            $one = db('article_cat')->where('id', $id)->find();
            builder('form')
                    ->addItem('pid', 'select', '上级', model('article_cat')->lists_select_tree(['id' => ['neq', $id]]))
                    ->addItem('title', 'input', '标题<font color="red">*</font>', '', 'lay-verify="required"')
                    ->addItem('tpl_lists', 'input_select', '列表', 'admin/index/tpl_select')
                    ->addItem('tpl_views', 'input_select', '详情', 'admin/index/tpl_select')
                    ->addItem('alias', 'input', '别名')
                    ->addItem('keywords', 'input', 'seo关键词')
                    ->addItem('description', 'textarea', 'seo描述')
                    ->build($one);


            return view();
        }
    }

    /**
     * @title 分类删除
     */
    public function category_delete($id) {
        empty($id) && $this->error('参数不能为空');

        if (db('article_cat')->where('pid', $id)->count()) {
            $this->error('请先删除子类');
        } elseif (db('article_cat')->where('id', $id)->delete() !== FALSE) {
            $this->success('', url('category'));
        } else {
            $this->error('删除失败');
        }
    }

}
